package org.ronghao.androidwifiplatfrom.utils;

import java.lang.reflect.Method;

public class Contants {

	/* 程序位于SD卡位置 */
	public static final String APP_LOCATION = "igame";
	public static final String APP_PICFILEPOS = "gamepics";
	public static final String APP_LOCATION_SET = "setting";
	
	/*系统设置*/
	public static final String APP_SETTING_ISSETTED = "isSetted";
	
	
	// /* 服务器地址 */
	// public static final String SERVER_HOST_IP = "192.168.1.143";
	/* 服务器端口 */
	public static final int SERVER_HOST_PORT = 8065;
	public static final int Client_HOST_PORT = 8066;
	/* 默认连接几次 */
	public static final int COUNTNUM = 10;

	public static final int MSG_FAILURE = 0;//通信失败
	public static final int MSG_SUCCESS = 1;//通信成功
	public static final int MSG_UPDATE = 3;// 单线通知
	public static final int MSG_BROADCAST = 4;// 广播通知
	public static final int MSG_UPDATESUCCESS = 5;//单线通知成功
	public static final int MSG_BROADCASTSUCCESS = 6;// 广播成功
	
	/*游戏图片大小*/
	public static final int GAME_PIC_WIDTH = 130;
	public static final int GAME_PIC_HEIGHT = 130;

	/* 动作信息 */
	public static final String MSG_ACTION_LOGIN = "login";
	public static final String MSG_ACTION_LOGOUT = "logout";
	public static final String MSG_ACTION_SENDMSG = "sendmessage";
	public static final String MSG_ACTION_ERROR = "error";

	/* 数据传输 */
	/* 通信消息 */
	public static final String COMMUNICATION_DATA = "data";
	public static final String COMMUNICATION_INDEX = "index";
	public static final String COMMUNICATION_SEND_CLIENT_IP = "send_client_ip";
	public static final String COMMUNICATION_SEND_CLIENT_NAME = "send_client_name";
	public static final String COMMUNICATION_TYPE = "type";
	public static final String COMMUNICATION_ACTION = "action";
	public static final String COMMUNICATION_MSG_DATA = "msg_data";
	/* 用户消息 */
	public static final String COMMUNICATION_USER = "user";
	public static final String COMMUNICATION_USER_NAME = "name";
	public static final String COMMUNICATION_USER_STYLE = "style";
	public static final String COMMUNICATION_USER_PIC = "pic";
	public static final String COMMUNICATION_USER_IP = "ip";
	/* 游戏 */
	public static final String COMMUNICATION_GAME = "game";
	public static final String COMMUNICATION_GAME_ID = "id";
	public static final String COMMUNICATION_GAME_GAMENAME = "game_name";
	public static final String COMMUNICATION_GAME_GAMEPASSWORD = "game_password";
	public static final String COMMUNICATION_GAME_GAMEID = "game_id";
	public static final String COMMUNICATION_GAME_GAMETOTLENUM = "game_totle_num";
	
	/*COMMUNICATION_GAME_HOST*/
	public static final String COMMUNICATION_GAME_HOST = "creater";

	/* 创建WIFI的错误 */
	public static final int CREATEWIFI_ERROR1 = -1;
	public static final int CREATEWIFI_ERROR2 = -2;
	public static final int CREATEWIFI_ERROR3 = -3;
	public static final int CREATEWIFI_ERROR4 = -4;
	public static final int CREATEWIFI_ERROR5 = -5;
	public static final int CREATEWIFI_ERROR6 = -6;

	/* 获得头像 */
	public static final String USERPIC = "userhead";

	public static String objToJsonString(Object obj) {
		// 初始化返回值
		String json = "str_empty";

		if (obj == null) {
			return json;
		}

		StringBuilder buff = new StringBuilder();
		Method methods[] = obj.getClass().getMethods();
		try {
			buff.append("[");
			buff.append("{");
			int i = 0;
			for (Method method : methods) {
				String methodName = method.getName();
				if (methodName.startsWith("get")
						&& !methodName.startsWith("getClass")) {
					if (i != 0) {
						buff.append(",");
					}

					String fieldName = methodName.substring(3,
							methodName.length());
					buff.append("\"" + fieldName + "\"");
					buff.append(":");
					Object value = method.invoke(obj, null, null);
					System.out.println("执行方法返回的值：" + value);
					if (value instanceof String) {
						buff.append("\"" + value + "\"");
					} else {
						buff.append(value);
					}
					i++;
				}
			}
			buff.append("}");
			buff.append("]");
			json = buff.toString();
		} catch (Exception e) {
			e.printStackTrace();
		}

		// try {
		// buff.append("[");
		// buff.append("{");
		// int i = 0;
		// for (Field field : fields) {
		//
		// if (i != 0) {
		// buff.append(",");
		// }
		// buff.append(field.getName());
		// buff.append(":");
		// buff.append("\"");
		// buff.append(field.get(obj) == null ? "" : field.get(obj));
		// buff.append("\"");
		// i++;
		// }
		// buff.append("}");
		// buff.append("]");
		// json = buff.toString();
		// } catch (Exception e) {
		// throw new RuntimeException("cause:" + e.toString());
		// }
		return json;
	}
}
